<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>生产派工编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script src="${ctx}/js/My97DatePicker/WdatePicker.js" type="text/javascript" ></script>
	<script src="${ctx}/js/print/LodopFuncs.js" type="text/javascript"></script>
	
<script type="text/javascript">
var basic_Url="${ctx}/logic/prodispat";
var grid;
var print = false;
var checai=false;
$(function(){
	$("#inputForm").validate({
		onsubmit: true,
		 /*执行ajaxsubmit  */ 
	    submitHandler: function(editform) {
	        //alert(jsonInfo); 
	        try{			    		
	            url =  basic_Url+"/save.do";
	            var api = frameElement.api;
	        	var W = api.opener;
	        	var d=new Date();
	        	var da=d.getFullYear()+'年'+(d.getMonth()+1)+'月'+(d.getDate())+'日';
	        	var obj={'zd004CustomerName':$("#zd004CustomerName").val(),'bizCreatorDate':da,'zd007SalesMan':$("#zd007SalesMan").val(),'zd003PiCode':$("#zd003PiCode").val(),'zd001FormCode':$("#zd001FormCode").val(),'zd009Remark':$("#zd009Remark").val(),'zhuguan':$("#zhuguan").val()}
	           	getReturnMessageThree(url,"inputForm",W.searchAP,'',obj,grid.getSelectedRows(),print,'prodispat',false,checai);
	        }catch(e){
	        	alert(e);
	        }
	    }
	});
	var billSt = '${prodispat.zd008Billstatus}';
	if(billSt=='PRODISPAT_ZAPPROVE' || billSt=='PRODISPAT_ZEND'){
		$("#forSubmit").hide();
	}
	grid = $("#dataGrid").ligerGrid({
    	columns: [{ display: 'ID', name: 'id', width: 1 , hide:true},
              { display: '产品id', name: 'zd006MaterialsId', width: 1 , hide:true},
			  { display: '<font color="gray">产品名称</font>', name: 'zd006MaterialsName', isSort: true, width: 60 },
			  { display: '<font color="gray">产品型号</font>', name: 'zd010MaterialsModel', isSort: true, width: 100},
			  { display: '<font color="gray">产品尺寸</font>', name: 'zd023MaterialsSize', isSort: true, width: 100},
			  { display: '<font color="gray">产品颜色</font>', name: 'zd026MaterialsColor', isSort: true, width: 100},
			  { display: '<font color="gray">材质</font>', name: 'zd027MaterialsQuality', isSort: true, width: 100},
			  { display: '工序id', name: 'zd029Process', width: 80, hide : true},
			  { display: '<font color="gray">工序名称</font>', name: 'zd029ProcessName', isSort: true, width: 80},
			  { display: '生产车间id', name: 'zd028WorkShop', width: 80, hide : true},
			  { display: '<font color="gray">订单数量</font>', name: 'zd015PlanQuantity', width: 80},
			  { display: '<font color="gray">已派工数量</font>', name: 'zd016ProductionQuantity', width: 80,render:function(item){
				  if(item.zd016ProductionQuantity==undefined||item.zd016ProductionQuantity==null||item.zd016ProductionQuantity==""){
					  return "0";
				  }
             	  return (1*item.zd016ProductionQuantity).toFixed(4);
              }},
			  { display: '待派工数量<font color="red">*</font>', name: 'zdTheQuantity', width: 80,editor : {type :'float'}},
			  { display: '单价<font color="red">*</font>', name: 'zd013Price', width: 80,editor : {type :'float'}},
			  { display: '人员code', name: 'zdUserCode', width: 80, hide:true},
			  { display: '工作人员<font color="red">*</font>', name: 'zdUserName', width: 80},
              { display: '<font color="gray">产品描述</font>', name: 'zd021MaterialsDescription', width: 150,editor: { type: 'text'}},
              { display: '<font color="gray">生产车间</font>', name: 'zd028WorkShopName', width: 80},
              { display: '<font color="gray">计量单位</font>', name: 'zd012MaterialsUnit', width: 60},
              { display: '<font color="gray">产品编码</font>', name: 'zd006MaterialsCode', isSort: true, width: 100}
              ],
              usePager: true,pageSize:1000,pageSizeOptions:[1000],
              enabledEdit: true,enabledSort:true,
              parms:[{ name : 'id' , value : '${prodispat.id}'}],
              url : basic_Url+"/gridItems.do",
              width: "100%", height:'90%', checkbox: true, rownumbers:true,  
              toolbar: { items: [
								 { text: '复制', click: addGridRow, icon: 'add' },
                                 { text: '选择工序', click: processWindow, icon: 'add' },
                                 { text: '选择人员', click: PersonWindow, icon: 'add' },
                                 { line: true },
                                 { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                                 ]
                       }
	});
	
	if($("#checai").val()=="yes"){
		checai=true;//是不是车裁车间
	}
}); 
//复制行
function addGridRow(){
	var cRows = grid.getCheckedRows();
	if(cRows.length==0){
		$.dialog.alert("请选择要复制的行！");
		return false;
	}
	var manager = $("#dataGrid").ligerGetGridManager();
	for(var i=0; i<cRows.length;i++){
		manager.addRow({
			zd006MaterialsId : cRows[i].zd006MaterialsId,
			zd006MaterialsCode : cRows[i].zd006MaterialsCode,
			zd006MaterialsName : cRows[i].zd006MaterialsName,
			zd010MaterialsModel : cRows[i].zd010MaterialsModel,
			zd023MaterialsSize : cRows[i].zd023MaterialsSize,
			zd026MaterialsColor : cRows[i].zd026MaterialsColor,
			zd027MaterialsQuality : cRows[i].zd027MaterialsQuality,
			zd012MaterialsUnit : cRows[i].zd012MaterialsUnit,
			zd029Process : '',
			zd029ProcessName : '',
			zd028WorkShop : '',
			zd028WorkShopName : '',
			zd015PlanQuantity : cRows[i].zd015PlanQuantity,
			zd013Price : cRows[i].zd013Price,
			zdTheQuantity : cRows[i].zd015PlanQuantity,
			zd021MaterialsDescription : cRows[i].zd021MaterialsDescription
		});
	}
	
}
//选择工序
function processWindow(){
	var cRows = grid.getCheckedRows();
	if(cRows==null||cRows.length==0){
		$.dialog.alert("请选择要关联的工序！");
		return false;
	}
	getDG().dialog({
		title:'选择工序',
		width : 600,
		height : 400,
		content: 'url:' + sys_context+'/public/windowalert/process.do?theExShop=Y',
		lock : true,
		max : true,
		min : true,
		ok: function(){
			var sRows = this.content.getSelected();
			if(sRows==null){
				$.dialog.alert("请选择数据");
				return false;
			}else{
				if(sRows.length>1){
					$.dialog.alert('只能选择一行数据!');
					return false;
				}else{
					var row = sRows[0];
					for(var i=0; i<cRows.length; i++){
						grid.updateCell('zd029Process', row.id, cRows[i]);
						grid.updateCell('zd029ProcessName', row.zd002Name, cRows[i]);
						grid.updateCell('zd028WorkShop', row.zd003WorkShop, cRows[i]);
						grid.updateCell('zd028WorkShopName', row.basicWorkshopDO.zd002Name, cRows[i]);
					}
				}
			}
			this.content.Close();
    		return false;
	    },
	    cancelVal: '关闭',
	    cancel: true 
	}).zindex();
}
//生产调度单列表
function PlanWindow(){
		getDG().dialog(
				{
					title : '选择生产调度单',
					width : 650,
					height : 350,
					content : 'url:' + sys_context
							+ '/public/windowalert/dispatch.do',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if (rows != null) {
							$(rows).each(function() {
								$("#zd004CustomerId").val(this.zd004CustomerId);
								$("#zd004CustomerCode").val(this.zd004CustomerCode);
								$("#zd004CustomerName").val(this.zd004CustomerName);
								$("#zd002OriCode").val(this.zd001FormCode);
								$("#zd003PiCode").val(this.zd003PiCode);
								var url=basic_Url+"/gridBomitems.do?id="+this.id;
								multipleSearch("#dataGrid",true,"",url);
							});
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
}

//人员列表
function PersonWindow(){
	var mrows = grid.getCheckedRows();
	if (mrows.length == 0) {
		$.dialog.alert("请选择要派工的工序！");
		return false;
	}
	getDG().dialog({
		title : '选择生产工人',
		width : 650,
		height : 350,
		content : 'url:' + sys_context
				+ '/public/windowalert/personnel.do?wspQuery=y',    
		lock : true,
		max : true,
		min : true,
		ok : function() {
			var rows = this.content.getSelected();
			if (rows != null) {
				$(mrows).each(function(i) {
					grid.updateCell('zdUserCode', rows[0].zd001Code, mrows[i]);
					grid.updateCell('zdUserName', rows[0].zd002Name, mrows[i]);
				});
			}
			this.content.Close();
			return false;
		},
		cancelVal : '关闭',
		cancel : true
	}).zindex();
}
//删除行
function deleteRow()
{ 
	grid.deleteSelectedRow();
}

function submitForm(){
	var cRows = grid.getCheckedRows();
	 if(cRows==null||cRows.length==0){
		 $.dialog.alert("请选择要派工的工序！");
		 return false;
	 }
	 var ba = true;
	 for (var i = 0;i<cRows.length;i++){
		var row= cRows[i]; 
		if (undefined==row.zd013Price||isNaN(row.zd013Price)|| row.zd013Price<= 0) {
			$.dialog.alert("第" + (i+1) + "行的工序单价没有填写");
			ba = false;
			return false;
		}
		if(undefined==row.zdTheQuantity||isNaN(row.zdTheQuantity)||row.zdTheQuantity<= 0){
	          $.dialog.alert("您第"+(i+1)+"行的“派工数量”不合法");
	          ba=false;
	          return false;
	       }
		if(row.zd015PlanQuantity<(row.zdTheQuantity+row.zd016ProductionQuantity)){
			$.dialog.alert("您第"+(i+1)+"行的“派工数量”大于“订单数量”无法派工");
	        ba=false;
	        return false;
		}
		if(undefined==row.zdUserCode||row.zdUserCode==null||row.zdUserCode ==""){
			$.dialog.alert("您第"+(i+1)+"行的“生产工人”不能为空无法派工");
	        ba=false;
	        return false;
		}
	}
	jQuery.ajaxSettings.async = false;
	if (ba) {
		$("#itemdata").val(JSON.stringify(cRows));
		print=true;
		$("#inputForm").submit();
	}
 }
//打印
function PrintView(type){
	var d=new Date();
	var da=d.getFullYear()+'年'+(d.getMonth()+1)+'月'+(d.getDate())+'日';
	var obj={'zd004CustomerName':$("#zd004CustomerName").val(),'bizCreatorDate':da,'zd007SalesMan':$("#zd007SalesMan").val(),'zd003PiCode':$("#zd003PiCode").val(),'zd001FormCode':$("#zd001FormCode").val(),'zd009Remark':$("#zd009Remark").val(),'zhuguan':$("#zhuguan").val()};
	var isprint=false;
	if(type==2){
		isprint=true;
	}
	window.parent.parent.loadPrint(obj,grid.getSelectedRows(),'prodispat',isprint,"",checai);
}
</script>

</head>

<body>

<input id="bizCreatorDate" type="hidden" name="bizCreatorDate" value='${prodispat.bizCreatorDate}' />
 <script>
if('${prodispat.bizCreatorDate}'==''){
	var date = new Date();
	$("#bizCreatorDate").val(date.getFullYear()+'-'+(date.getMonth()+1)+'-'+(date.getDate()));
}

</script> 
<input id="zhuguan" type="hidden" value="${user.realName}" />
<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" readonly="readonly" value="${prodispat.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${prodispat.bizVersion}"/>
<input type="hidden" id="zd002SaleId" name="zd002SaleId" value="${prodispat.zd002SaleId}" />
<input type="hidden" id="itemdata" name="itemdata"/>
<input type="hidden" id="checai" value="${ccFlag}" />
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
         			<c:if test="${not empty prodispat.zd001FormCode}">
	         			<tr>
	                    	<td>生产派工单号:</td>
	   		                 <td colspan="3">
	   		                	<input type="text" id="zd001FormCode" name="zd001FormCode" readonly="readonly" style="width: 40%;"  value="${prodispat.zd001FormCode}"/>
	   		                 </td>
	   		           </tr>
   		           </c:if>
   		           <tr>
   		           		<c:choose>
   		           			<c:when test="${empty prodispat.zd001FormCode}">
   		           				<td >生产调度单号:</td>
		   		                <td>
		   		                	<input type="text" id="zd002OriCode" name="zd002OriCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd002OriCode}" />
		   		                 	<input type="button" value="..." onclick="PlanWindow();"/>
		   		                </td>
   		           			</c:when>
   		           			<c:otherwise>
   		           				<td >生产调度单号:</td>
		   		                <td>
		   		                	<input type="text" id="zd002OriCode" name="zd002OriCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd002OriCode}" />
		   		                </td>
   		           			</c:otherwise>
   		           		</c:choose>
   		                <td >PI编号:</td>
   		                <td>
   		                	<input type="text" id="zd003PiCode" name="zd003PiCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd003PiCode}" />
   		                </td>
   		           </tr>
                   <tr>
   		             <td >客户编码:</td>
   		                 <td>
   		                 <input type="hidden" id="zd004CustomerId" name="zd004CustomerId" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerId}" />
   		                <input type="text" id="zd004CustomerCode" name="zd004CustomerCode" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerCode}" />
   		                 </td>  
   		              <td >客户名称:</td>
   		                 <td>
   		                <input type="text" id="zd004CustomerName" name="zd004CustomerName" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd004CustomerName}" />
   		                 </td>     
   		           </tr>
   		           <tr>
                    	<td >订单日期:</td>
   		                 <td>
   		                	<input type="text" id="zd005OrderDate" name="zd005OrderDate" readonly="readonly" style="width: 40%;" class="required" class="Wdate" value="<fmt:formatDate value="${prodispat.zd005OrderDate}" pattern="yyyy-MM-dd" type="both" />"/>
   		                 </td>
   		                 <td >交货日期:</td>
   		                 <td>
   		                	<input type="text" id="zd006DeliveryDate" name="zd006DeliveryDate" readonly="readonly" style="width: 40%;" class="Wdate" value="<fmt:formatDate value="${prodispat.zd006DeliveryDate}" pattern="yyyy-MM-dd" type="both" />"/>
   		                 </td>
   		           </tr>
   		           <tr>
   		           		<td >业务员:</td>
   		                 <td>
   		                	<input type="text" id="zd007SalesMan" name="zd007SalesMan" class="required" readonly="readonly" style="width: 40%;" value="${prodispat.zd007SalesMan}" />
   		                 </td>
   		                 <td >附件:</td>
   		           		<td>
   		           			<input type="button" value="查看附件..." onclick="uploadFileWindow('zd002SaleId','paigong');" />
   		           		</td>
   		           </tr>
   		           <tr>
   		           		<td >备注:</td>
   		           		<td colspan="3"> <input id="zd009Remark" name="zd009Remark" style="width: 40%;" value="${prodispat.zd009Remark}" /></td>
   		           </tr>
   		           <tr>
						<td colspan="4" style="margin-bottom: 10px;">
							<div id="dataGrid"></div>
						</td>
					</tr>
                </table>               
         </div>
         <div class="formBar">
	        <ul>
	        	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(1);">打印预览</button></div></div></li>
		      	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(2);">打印</button></div></div></li>
		       		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSubmit" onclick="submitForm()">保存</button></div></div></li>
		       	<li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>

</body>
</html>
